package OutputWriter;

use JIP::ToolSet; # load my commonly-used modules in a single import
use parent qw( Class::Base Class::Accessor::Fast );

use IO::File;
use Carp qw( croak );
use Readonly qw( Readonly );
use English qw( -no_match_vars );

__PACKAGE__->mk_ro_accessors( qw( _outputFile _outputHandle ) );

Readonly my $EOL => "\n"; # end of line

sub init {
    my( $self, $config ) = @ARG;

    $self->{'_outputFile'} = $config->{'output_file'}
        or croak 'cannot init mandatory option "output_file"';

    $self->{'_outputHandle'} = IO::File->new( $self->{'_outputFile'}, 'w' )
        or croak 'cannot open output file: '. $OS_ERROR;

    return $self;
}

sub binmode {
    my( $self, $layer ) = @ARG;

    croak('required parameter not defined') unless $layer;

    $self->_outputHandle->binmode( $layer );
}

sub _write {
    $ARG[0]->_outputHandle->print( $ARG[1] . $EOL )
        or croak 'cannot print: '. $OS_ERROR;
}

1;
